<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@page isELIgnored="false" %><%--启动EL表达式解析--%>
<%
    request.setCharacterEncoding("UTF-8");
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    String goods_edit_url = basePath+"pages/back/admin/editview";
    String goods_delete_url = basePath+"pages/back/admin/delete";
    String goods_add_url = basePath +"pages/back/admin/good_add";
%>
<html>
<head>
    <base href="<%=basePath%>">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>商品列表</title>
    <link rel="stylesheet" type="text/css" href="<%=basePath%>/bootstrap/css/bootstrap.min.css">
    <!-- href：里面的路径是你导入在static文件夹里面下面bootstrap.min.css所在的路径，下面两个属性一样 -->
    <script type="text/javascript" src="<%=basePath%>/jquery/jquery-3.6.0.min.js"></script>
    <script type="text/javascript" src="<%=basePath%>/bootstrap/js/bootstrap.min.js"></script>
</head>
<body class="panel-body">
    <form class="form-inline col-md-offset-5" action="<%=basePath%>pages/back/admin/list">
        <div class="form-group">
            <input type="text" class="form-control" id="kw" name="kw" placeholder="输入搜索词">
        </div>
        <div class="form-group">
            <button type="submit" class="btn btn-primary">搜索</button>
        </div>
    </form>
    <table class="table table-striped table-bordered table-hover">
        <tr>
            <td style="width: 5%"><input type="checkbox" id="selectAll"/></td>
            <td>商品名称</td>
            <td>商品单价</td>
            <td>商品分类</td>
            <td>操作</td>
        </tr>
        <c:forEach items="${allGoods}" var="goods">
            <tr>
                <td><input type="checkbox" name="gid" value="${goods.gid}"/></td>
                <td>${goods.name}</td>
                <td><fmt:formatNumber value="${goods.price}"/></td>
                <td>${allItems[goods.item.iid]}</td>
                <td>
                    <a href="<%=goods_edit_url%>?gid=${goods.gid}" class="btn btn-warning btn-xs">
                    <span class="glyphicon glyphicon-pencil"></span>&nbsp;编辑</a>
                </td>
            </tr>
        </c:forEach>
    </table>
    <a href="<%=goods_add_url%>" class="btn btn-primary btn-lg active" role="button">新增商品</a>
    <a href="<%=goods_delete_url%>" class="btn btn-primary btn-lg active" role="button" id="deleteSelect">
        <span class="glyphicon glyphicon-trash"></span>&nbsp;删除选中信息</a>

    <nav aria-label="...">
        <ul class="pager">
            <li><a href="<%=basePath%>pages/back/admin/list?cp=${currentPage==1?1:currentPage-1}">上一页</a></li>
            <li><a>当前第${currentPage}页/共${totalPages}页</a></li>
            <li><a href="<%=basePath%>pages/back/admin/list?cp=${currentPage==totalPages?totalPages:currentPage+1}">下一页</a></li>

        </ul>
    </nav>
</body>
<script>
    $("#selectAll").click(function (){
        $("#selectAll")[0].checked
        let selector = $("input[name='gid']");
        for(let i=0;i<selector.length;i++){
            selector[i].checked = $("#selectAll")[0].checked;
        }
    });
    $("#deleteSelect").click(function (){
        let checkedArray = new Array();
        let selector = $("input[name='gid']");
        for(let i=0;i<selector.length;i++){
            if(selector[i].checked == true){
                checkedArray.push(parseInt(selector[i].value))
            }
        }
        let url = $("#deleteSelect").attr("href");
        $("#deleteSelect").attr("href",url+"?ids="+checkedArray.toString())
    });

</script>
</html>
